<template>
  <rx-dialog @ok="handleSubmit" @cancel="cancel">
    <rx-layout>
      <template #middle>
        <a-form ref="form" :model="mdl" :rules="rules" layout="vertical" class="rx-a-form-vertical">
          <a-row>
            <a-col :span="24">
              <a-form-item  :label="i18n('form.note','申请理由')">
                <a-textarea v-model="mdl.note" :rows="8"/>
              </a-form-item>
            </a-col>
          </a-row>
          <a-row>
            <a-col :span="24">
              <a-form-item  :label="i18n('form.orgChoice','机构选择')">
                <rx-grid ref="table"
                         :hasPage="false"
                         :selectConfig="selectConfig"
                         :data="dataSource"
                         field="instId"
                         :columns="columns"
                         :height="'auto'"
                >
                </rx-grid>
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>
      </template>
    </rx-layout>
  </rx-dialog>
</template>
<script>
  import OsInstApi from "@/common/api/user/osInst";
  import {BaseFormModel, RxDialog, RxGrid, Util} from '@lowcode/jpaas-base-lib';
  import {mapState} from "pinia";
  import userUser from "@/stores/userUser";
  export default {
    name: 'OsInstJoinInstEdit',
    mixins:[BaseFormModel],
    components: {
      RxDialog,
      RxGrid
    },
    data(){
      return {
        rules: {
          note: [{required: true, message: "请输入申请理由", trigger: 'change'}],
        },
        selectConfig: {
          type: 'checkbox',
        },
        columns:[{title: this.i18n('form.nameCn','机构中文名'), field: 'nameCn', minWidth: 100},
          {title: this.i18n('form.domain','机构域名'), field: 'domain', minWidth: 100}],
        dataSource:[]
      }
    },
    computed:{
      ...mapState(userUser,['user'])
    },
    methods: {
      i18n(name,text,key){
          return this.$ti18(name,text,"OsInstShowCanApply",key);
      },
      onload_(){
        OsInstApi.getList().then(result=>{
          this.dataSource = result.data;
        })
      },
      save(values){
        if(this.user.admin){
            this.$message.error(this.i18n('msa.isAdminTips','管理员不能申请加入其他机构!'));
            return new Promise(resolve => {
                resolve({success:false});
            })

        }
        var data=[];
        var rows=this.$refs.table.getSelectedRows();
        if(rows.length==0){
          this.$message.error(this.i18n('msa.selectRows','请选择至少一个机构!'));
          return new Promise(resolve => {
            resolve({success:false});
          })
        }
        for(var i=0;i<rows.length;i++){
          data.push({instId:rows[i].instId,moreInstNote:values.note});
        }
        return OsInstApi.saveSelectInst(data);
      }
    }
  }
</script>
